package com.app.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.Application;
import com.app.R;
import com.app.cache.SecondLevelCache;
import com.app.constant.Constants;
import com.app.constant.KeyValue;
import com.app.http.HttpDataApi;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.bean.User;
import com.app.model.request.ReqObj;
import com.app.model.response.ResObj;
import com.app.utils.LoginUtil;
import com.app.utils.ToastUtil;
import com.app.view.RoundImageView;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ActivityMine extends AppCompatActivity implements HttpDataApi.OnRequestCallback {

    private static final String LOG_TAG = ActivityMine.class.getSimpleName();
    @BindView(R.id.imgPortrait)
    RoundImageView imgPortrait;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.rlInfo)
    RelativeLayout rlInfo;
    @BindView(R.id.llOrders)
    LinearLayout llOrders;
    @BindView(R.id.walletContainer)
    LinearLayout walletContainer;
    @BindView(R.id.ivShare)
    ImageView ivDate;
    @BindView(R.id.tvShare)
    TextView tvDate;
    @BindView(R.id.llShare)
    LinearLayout llDate;
    @BindView(R.id.ivInvitecode)
    ImageView ivDateMine;
    @BindView(R.id.tvInvitecode)
    TextView tvDateMine;
    @BindView(R.id.llInvitecode)
    LinearLayout llDateMine;
    @BindView(R.id.ivAboutus)
    ImageView ivAboutus;
    @BindView(R.id.tvAboutus)
    TextView tvAboutus;
    @BindView(R.id.llAboutus)
    LinearLayout llAboutus;
    @BindView(R.id.ivContactus)
    ImageView ivContactus;
    @BindView(R.id.tvContactus)
    TextView tvContactus;
    @BindView(R.id.llContactus)
    LinearLayout llContactus;
    @BindView(R.id.ivSetting)
    ImageView ivSetting;
    @BindView(R.id.tvSetting)
    TextView tvSetting;
    @BindView(R.id.llSetting)
    LinearLayout llSetting;
    @BindView(R.id.refreshLayout)
    LinearLayout refreshLayout;


    private String token = null;

    private HttpDataLoader dataLoader;

    private User user;

    public ActivityMine() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        ButterKnife.bind(this);
        dataLoader = new HttpDataLoader(this);
        initView();
    }

    private void initView() {
        token = LoginUtil.getToken();
        if (token != null) {
            llSetting.setVisibility(View.VISIBLE);
            tvName.setText(LoginUtil.getLoginName());
        } else {
            tvName.setText("立即登录");
            llSetting.setVisibility(View.GONE);
        }
    }




    @OnClick({R.id.ivBack,R.id.llInvitecode, R.id.llShare, R.id.tvName, R.id.llActive, R.id.rlInfo, R.id.llOrders, R.id.walletContainer,R.id.llAboutus, R.id.llContactus, R.id.llSetting})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                overridePendingTransition(0, R.anim.push_left_out);
                break;
            case R.id.tvName:
                if (token == null) {
                    intent = new Intent(ActivityMine.this, ActivityLogin.class);
                }
//                else {
//                    intent = new Intent(ActivityMine.this, ActivityWallet.class);
//                }
                break;
            case R.id.walletContainer:
                if (token == null) {
                    intent = new Intent(ActivityMine.this, ActivityLogin.class);
                } else {
                    intent = new Intent(ActivityMine.this, ActivityWallet.class);
                }
                break;
            case R.id.llOrders:
                if (token == null) {
                    intent = new Intent(ActivityMine.this, ActivityLogin.class);
                } else {
                    intent = new Intent(ActivityMine.this, ActivityRecords.class);
                }
                break;
            case R.id.llAboutus:
                intent = new Intent(ActivityMine.this, ActivityAboutUs.class);
                break;
            case R.id.llShare:
                intent = new Intent(ActivityMine.this, ActivityInvite.class);
                break;
            case R.id.llInvitecode:
                intent = new Intent(ActivityMine.this, ActivityInvitecode.class);
                break;
            case R.id.llActive:
                intent = new Intent(ActivityMine.this, ActivityActive.class);
                break;
            case R.id.llContactus:
                intent = new Intent(ActivityMine.this, ActivityContactUs.class);
                break;
            case R.id.llSetting:
                AlertDialog.Builder builder = new AlertDialog.Builder(ActivityMine.this);
                builder.setMessage("确认退出吗？");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LoginUtil.logout();
                        dialog.dismiss();
                        startActivity(new Intent(ActivityMine.this, ActivityLogin.class));
                        finish();
                        overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                break;
            default:
                break;
        }
        if (intent != null) {
            startActivity(intent);
            overridePendingTransition(R.anim.push_right_in, 0);
        }
    }


    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        Log.e(LOG_TAG, "onRequestSuccess");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "result: " + result);
//        if (url.equals(HttpDataReqUrl.URL_GET_MY_INFO)) {
//            if (result.getCode() == Constants.CODE_SUCCESS) {
//                if (result.getData() instanceof User) {
//                    user = (User) result.getData();
//                    SecondLevelCache.sharedInstance().put(KeyValue.USER, user);
//                    if (user.getPhone() != null && !user.getPhone().equals("")) {
//                        tvName.setText(user.getPhone());
//                    }
//                } else {
//                    ToastUtil.getInstance().showToast(ActivityMine.this, result.getMsg());
//                }
//            } else {
//                ToastUtil.getInstance().showToast(ActivityMine.this, result.getMsg());
//            }
//        }
    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        Log.e(LOG_TAG, "onRequestError");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "statusCode: " + statusCode);
        ToastUtil.getInstance().showToast(ActivityMine.this, error);
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, 0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(0, R.anim.push_left_out);
    }
}
